﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JCTimes.DomainModel.Repository;
using System.Web.Mvc;
using JCTimes.DomainModel.Entity;
using JCTimes.Portal.Models;

namespace JCTimes.Portal.Infrastructure
{
    /// <summary>
    /// Session 操作类
    /// 1、GetSession(string name)根据session名获取session对象
    /// 2、SetSession(string name, object val)设置session
    /// </summary>
    public class SessionHelper
    {
        private static IUserRepository uRep = (IUserRepository)DependencyResolver.Current.GetService(typeof(IUserRepository));
        private static IRoleRepository roleRep = (IRoleRepository)DependencyResolver.Current.GetService(typeof(IRoleRepository));

        public static UserInfo CurrUser
        {
            get
            {
                var currUser = GetSession("CurrUser") as UserInfo;

                // TODO: Mock session, please remove these code when dev complete
                if (currUser == null)
                {
                    //判断cookie信息是否存在 如存在自动登录
                    string username = CookieHelper.GetCookieValue("JCTimesUserName");
                    string password = CookieHelper.GetCookieValue("JCTimesPassword");
                    if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
                    {
                        var user = uRep.Find(m => m.UserName == username && m.Password == password && m.Flag == 0);
                        if (user != null)
                        {
                            var role = roleRep.Find(user.RoleID);
                            var userInfo = new UserInfo
                            {
                                UserID = user.ID,
                                UserName = user.UserName,
                                Name = user.Name,
                                RoleID = user.RoleID,
                                RoleName = (role != null ? role.RoleName : null),
                                ParentID = user.ParentID
                            };
                            SessionHelper.CurrUser = userInfo;
                            currUser = GetSession("CurrUser") as UserInfo;
                        }
                    }
                }
                return currUser;
            }
            set
            {
                SetSession("CurrUser", value);
            }
        }

        /// <summary>
        /// 根据session名获取session对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static object GetSession(string name)
        {
            return HttpContext.Current.Session[name];
        }

        /// <summary>
        /// 设置session
        /// </summary>
        /// <param name="name">session 名</param>
        /// <param name="val">session 值</param>
        public static void SetSession(string name, object val)
        {
            HttpContext.Current.Session.Remove(name);
            HttpContext.Current.Session.Add(name, val);
        }

        /// <summary>
        /// 添加Session，调动有效期为20分钟
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <param name="strValue">Session值</param>
        public static void Add(string strSessionName, string strValue)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = 20;
        }

        /// <summary>
        /// 添加Session，调动有效期为60分钟
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <param name="strValues">Session值数组</param>
        public static void Adds(string strSessionName, string[] strValues)
        {
            HttpContext.Current.Session[strSessionName] = strValues;
            HttpContext.Current.Session.Timeout = 60;
        }

        /// <summary>
        /// 添加Session
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <param name="strValue">Session值</param>
        /// <param name="iExpires">调动有效期（分钟）</param>
        public static void Add(string strSessionName, string strValue, int iExpires)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = iExpires;
        }

        /// <summary>
        /// 添加Session
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <param name="strValues">Session值数组</param>
        /// <param name="iExpires">调动有效期（分钟）</param>
        public static void Adds(string strSessionName, string[] strValues, int iExpires)
        {
            HttpContext.Current.Session[strSessionName] = strValues;
            HttpContext.Current.Session.Timeout = iExpires;
        }

        /// <summary>
        /// 读取某个Session对象值
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <returns>Session对象值</returns>
        public static string Get(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return HttpContext.Current.Session[strSessionName].ToString();
            }
        }

        /// <summary>
        /// 读取某个Session对象值数组
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <returns>Session对象值数组</returns>
        public static string[] Gets(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return (string[])HttpContext.Current.Session[strSessionName];
            }
        }

        /// <summary>
        /// 删除某个Session对象
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        public static void Del(string strSessionName)
        {
            HttpContext.Current.Session[strSessionName] = null;
        }
    }
}